/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.social;

import com.nc.kyiv.yellow.sociallibrary.POJO.LibUser;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author My
 */
public class GooglePlus extends OAuthClient {

    final String CLIENT_ID = "1039543450442-ivng6gddcsp44fidhus8svdd5uj09vgj.apps.googleusercontent.com";
    final String CLIENT_SECRET = "aZIUGezvk8T7eTt3Pb2jdoMK";
    final String REDIRECT_URI = "http://localhost:8080/Social_Library/GooglePlus";
    final String MAIN_PAGE_URL = "http://localhost:8080/Social_Library";
    final String URL_FOR_ACCESS_TOKEN = "https://accounts.google.com/o/oauth2/token";
    final String URL_BEGINNIG_FOR_PERSONAL_DATA = "https://www.googleapis.com/userinfo/v2/me?access_token=";
    final String URL_BEGINNIG_FOR_BIRTHDAY = "https://www.googleapis.com/plus/v1/people/me?access_token=";
    final String GRANT_TYPE = "authorization_code";
    final String BIRTHDAY_DATE_FORMAT = "yyyy-MM-dd";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        StringBuilder params = new StringBuilder();
        params.append("client_id=");
        params.append(CLIENT_ID + "&");
        params.append("client_secret=");
        params.append(CLIENT_SECRET + "&");
        params.append("code=");
        params.append(req.getParameter("code"));
        params.append("&");
        params.append("redirect_uri=");
        params.append(REDIRECT_URI + "&");
        params.append("grant_type=");
        params.append(GRANT_TYPE);

        String rawAccessToken = getStringFromHTMLwithPostMethod(URL_FOR_ACCESS_TOKEN, params.toString());
        String accessToken = (String) parseJSON(rawAccessToken).get("access_token");
        String tokenType = (String) parseJSON(rawAccessToken).get("token_type");
        
        StringBuilder urlForPersonalData = new StringBuilder();
        urlForPersonalData.append(URL_BEGINNIG_FOR_PERSONAL_DATA);
        urlForPersonalData.append(accessToken + "&");
        urlForPersonalData.append("access_token_type=");
        urlForPersonalData.append(tokenType);

        String urlForBirthday = urlForPersonalData.toString().replace(URL_BEGINNIG_FOR_PERSONAL_DATA, URL_BEGINNIG_FOR_BIRTHDAY);
        
        String personalData = getStringFromHTMLwithGetMethod(urlForPersonalData.toString());
        String birthdayData = getStringFromHTMLwithGetMethod(urlForBirthday.toString());

        Map<String, String> personalDataMap = parseJSON(personalData);
        String pureBirthday = parseJSON(birthdayData).get("birthday");
        
        LibUser user = new LibUser("111", personalDataMap.get("id"), personalDataMap.get("given_name"),
                personalDataMap.get("family_name"), personalDataMap.get("email"));
        if (personalDataMap.get("gender") != null) {
            switch (personalDataMap.get("gender")) {
                case "male":
                    user.setGender(2);
                    break;
                case "female":
                    user.setGender(1);
                    break;
                default:
                    user.setGender(0);
                    break;
            }
        }
        if (pureBirthday != null) {
            Date birthday = getUnifiedBirthDate(pureBirthday, BIRTHDAY_DATE_FORMAT);
            user.setBirthDate(birthday);
        }
        save(user);
        login(user, req);
        
        resp.sendRedirect(MAIN_PAGE_URL); 

    }


    @Override
    protected String getPureAccessToken(String urlForAccessToken) {
        return null;
    }


}
